import service from "../../mock/manageMock"
const manageModule = {
    namespaced:true,
    state:{
        nameList:["产品图","产品名称","产品编号","添加日期","浏览量","搜索量","状态","操作"],
        cartLiat:[],
        flag:""
    },
    getters: {},
    mutations: {
        //加载获取
        getCartList(state,zaihe){
            state.cartLiat = zaihe.data
        },
        // 修改
        upCartList(state,zaihe){
            if(state.flag===""){
                state.cartLiat = zaihe.data
            }
        },
        //删除
        delCartList(state,zaihe){
            state.cartLiat = zaihe.data
            state.flag=""
        },
        //搜索
        search(state,zaihe){
            let name = zaihe;
            let reg = new RegExp(name);
            state.cartLiat = state.cartLiat.filter((item)=>{
                if(reg.test(item.id)||reg.test(item.cartname)){
                    return item
                }
            })
        },
        //修改商品信息
        updatacart(state,value){
            if(state.flag===""){
                state.cartLiat = value.data
            }
        },
        //标记
        biaoji(state,value){
            state.flag = value.flag
        },
        //添加
        adddata(state,value){
            state.cartLiat = value.data
        }
    },
    actions:{
        //获取列表
        async getListAsync({commit}){
            const res = await service.getDate();
            commit("getCartList",res.result)
        },
        //修改状态
        async upListAsync({commit},data){
            const res = await service.upDate(data.value, data.id);
            commit("upCartList",res.result)
        },
        //删除商品
        async delListAsync({commit},name){
            const res = await service.delDate(name);
            commit("delCartList",res.result)
        },
        //修改信息
        async updataAsync({commit},form){
            const res = await service.upDatecart(form.data);
            commit("updatacart",res.result)
        },
        async adddataAsync({commit},form){
            const res = await service.addCart(form);
            console.log(res.result);
            commit("adddata",res.result)
        }
    }
}
export default manageModule
